/*
 * @Author: Setsuna
 * @Email: 601890383@qq.com
 * @FilePath: \src\components\ApproveModal\index.tsx
 * @Date: 2021-09-23 17:57:16
 * @LastEditTime: 2022-06-21 18:03:31
 */
import { http, UdModal } from '@/lib'
import React from 'react'
import SetApproveModal from '@/components/SetApproveModal'
import todoApi from '@/apis/TodoList/index'
import { message } from 'antd'

export default data => {
  let uploadFiles = []
  const updateFiles = files => {
    uploadFiles = files
  }
  const userId = JSON.parse(sessionStorage.getItem('UaaSysInfo'))?.profile?.id
  let url = todoApi.complete
  UdModal.open({
    title: '审批',
    content: <SetApproveModal params={data} updateFiles={updateFiles} />,
    onOk: values => {
      // url和type必传，type固定'url'
      const attachments = uploadFiles?.map(({ url, name }) => ({ url, name, type: 'url' }))
      const { taskId, roleIds, refresh, todoType, isCross, standardNum, quotaNum, e } = data
      const { isValid, comment, approveUserIds } = values
      let params = { taskId, roleIds, userId, attachments, comment, approveUserIds, isCross, standardNum, quotaNum }
      if (isValid === 0) {
        // 将选择的审批人参数处理
        params['workflowReqVos'] = {
          name: 'candidateUsers', // name传固定值
          userId: params['approveUserIds'],
        }
        delete params['approveUserIds']
      } else {
        url = todoApi.processReject
      }
      e.drawerBtnLoading()
      return http
        .post(url, params)
        .then(res => {
          message.success('操作成功')
          setTimeout(() => {
            refresh(todoType)
            e.close()
          }, 1300)
        })
        .finally(() => {
          e.drawerBtnLoadingHide()
        })
    },
  })
}
